﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WindowsLive;

namespace goMatic
{
    public partial class Registration : System.Web.UI.Page
    {
        const string LoginCookie = "webauthtoken";

        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);
        static dbEntities oDB;
        protected static string AppId = wll.AppId;
        protected string UserId;

        protected void Page_Load(object sender, EventArgs e)
        {
            /* If the user token has been cached in a site cookie, attempt
               to process it and extract the user ID. */
            if (!Page.IsPostBack)
            {
                HttpRequest req = HttpContext.Current.Request;
                HttpCookie loginCookie = req.Cookies[LoginCookie];
                dbEntities oDB = new dbEntities();
                if (loginCookie != null)
                {
                    string token = loginCookie.Value;

                    if (!string.IsNullOrEmpty(token))
                    {
                        WindowsLiveLogin.User user = wll.ProcessToken(token);

                        if (user != null)
                        {
                            UserId = user.Id;
                            ViewState.Add("UserID", UserId);

                            if (oDB.Customers.Count(t => t.liveid == user.Id) == 0)
                            {
                                Button1.Enabled = true;
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                UserId = ViewState["UserID"].ToString();
                if (string.IsNullOrEmpty(UserId))
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(UserId))
                {
                    dbEntities oDB = new dbEntities();
                    Customers oNew = oDB.Customers.Create();
                    oNew.liveid = UserId;
                    oNew.customerid = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Ticks.ToString();
                    oNew.C__createdAt = DateTime.Now;
                    oNew.firstname = tbFirstName.Text;
                    oNew.familyname = tbName.Text;
                    oNew.mail = tbMail.Text;
                    
                    oDB.Customers.Add(oNew);
                    oDB.SaveChanges();
                    ViewState.Add("CustomerID", oNew.customerid);

                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            Response.Redirect("~/Start.aspx");
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (CheckBox1.Checked)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}